import java.util.LinkedList;
import java.util.Queue;
//https://leetcode.cn/problems/implement-stack-using-queues/
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (queue1.isEmpty() && queue2.isEmpty()) {
            queue1.offer(x);
            return;
        }
        if (!queue1.isEmpty()) queue1.offer(x);
        if (!queue2.isEmpty()) queue2.offer(x);
    }

    public int pop() {
        if (queue1.isEmpty()) {
            while (queue2.size() != 1) queue1.offer(queue2.remove());
            return queue2.remove();
        }else {
            while (queue1.size() != 1) queue2.offer(queue1.remove());
            return queue1.remove();
        }
    }

    public int top() {
        if (queue1.isEmpty()) {
            while (queue2.size() != 1) queue1.offer(queue2.remove());
            int x = queue2.remove();
            queue1.offer(x);
            return x;
        }else {
            while (queue1.size() != 1) queue2.offer(queue1.remove());
            int x = queue1.remove();
            queue2.offer(x);
            return x;
        }
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}